package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharShrineList;
import org.gdstash.character.GDCharUID;
import org.gdstash.db.DBShrine;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractShrinePane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class GDCharShrineAct2Pane extends GDAbstractShrinePane {
    private JCheckBox cbSpinedCove;
    private JCheckBox cbRockyCoast;
    private JCheckBox cbCronleysHideout;
    private JCheckBox cbOldArkovia;
    private JCheckBox cbArkovianUndercity;
    private JCheckBox cbBrokenHills;
    private JCheckBox cbStepsOfTorment;
    private int difficulty;
    private boolean changed;

    public GDCharShrineAct2Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbSpinedCove).addComponent(this.cbRockyCoast).addComponent(this.cbCronleysHideout).addComponent(this.cbOldArkovia).addComponent(this.cbArkovianUndercity).addComponent(this.cbBrokenHills).addComponent(this.cbStepsOfTorment));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbSpinedCove)).addGroup(layout.createParallelGroup().addComponent(this.cbRockyCoast)).addGroup(layout.createParallelGroup().addComponent(this.cbCronleysHideout)).addGroup(layout.createParallelGroup().addComponent(this.cbOldArkovia)).addGroup(layout.createParallelGroup().addComponent(this.cbArkovianUndercity)).addGroup(layout.createParallelGroup().addComponent(this.cbBrokenHills)).addGroup(layout.createParallelGroup().addComponent(this.cbStepsOfTorment));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbSpinedCove, this.cbRockyCoast);
        layout.linkSize(0, this.cbSpinedCove, this.cbCronleysHideout);
        layout.linkSize(0, this.cbSpinedCove, this.cbOldArkovia);
        layout.linkSize(0, this.cbSpinedCove, this.cbArkovianUndercity);
        layout.linkSize(0, this.cbSpinedCove, this.cbBrokenHills);
        layout.linkSize(0, this.cbSpinedCove, this.cbStepsOfTorment);
        layout.linkSize(1, this.cbSpinedCove, this.cbRockyCoast);
        layout.linkSize(1, this.cbSpinedCove, this.cbCronleysHideout);
        layout.linkSize(1, this.cbSpinedCove, this.cbOldArkovia);
        layout.linkSize(1, this.cbSpinedCove, this.cbArkovianUndercity);
        layout.linkSize(1, this.cbSpinedCove, this.cbBrokenHills);
        layout.linkSize(1, this.cbSpinedCove, this.cbStepsOfTorment);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_ACT2"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbSpinedCove == null) {
            (this.cbSpinedCove = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbSpinedCove.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_02_SPINED_COVE"));
        this.cbSpinedCove.setFont(fntCheck);
        if (this.cbRockyCoast == null) {
            (this.cbRockyCoast = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbRockyCoast.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_02_ROCKY_COAST"));
        this.cbRockyCoast.setFont(fntCheck);
        if (this.cbCronleysHideout == null) {
            (this.cbCronleysHideout = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCronleysHideout.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_02_CRONLEYS_HIDEOUT"));
        this.cbCronleysHideout.setFont(fntCheck);
        if (this.cbOldArkovia == null) {
            (this.cbOldArkovia = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbOldArkovia.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_02_OLD_ARKOVIA"));
        this.cbOldArkovia.setFont(fntCheck);
        if (this.cbArkovianUndercity == null) {
            (this.cbArkovianUndercity = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbArkovianUndercity.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_02_ARKOVIAN_UNDERCITY"));
        this.cbArkovianUndercity.setFont(fntCheck);
        if (this.cbBrokenHills == null) {
            (this.cbBrokenHills = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBrokenHills.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_02_BROKEN_HILLS"));
        this.cbBrokenHills.setFont(fntCheck);
        if (this.cbStepsOfTorment == null) {
            (this.cbStepsOfTorment = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbStepsOfTorment.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_02_STEPS_OF_TORMENT"));
        this.cbStepsOfTorment.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbSpinedCove.setSelected(selected);
        this.cbRockyCoast.setSelected(selected);
        this.cbCronleysHideout.setSelected(selected);
        this.cbOldArkovia.setSelected(selected);
        this.cbArkovianUndercity.setSelected(selected);
        this.cbBrokenHills.setSelected(selected);
        this.cbStepsOfTorment.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getShrineList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbSpinedCove.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_02_SPINED_COVE);
        }
        if (this.cbRockyCoast.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_02_ROCKY_COAST);
        }
        if (this.cbCronleysHideout.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_02_CRONLEYS_HIDEOUT);
        }
        if (this.cbOldArkovia.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_02_OLD_ARKOVIA);
        }
        if (this.cbArkovianUndercity.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_02_ARKOVIAN_UNDERCITY);
        }
        if (this.cbBrokenHills.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_02_BROKEN_HILLS);
        }
        if (this.cbStepsOfTorment.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_02_STEPS_OF_TORMENT);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        boolean expFG = GDStashFrame.expansionForgottenGods;
        if (gdc != null) {
            expFG = (expFG || gdc.isForgottenGodsChar());
        }
        DBShrine.Info info = null;
        info = DBShrine.getShrineInfo("devotionshrineb01.dbr", this.difficulty);
        this.cbSpinedCove.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineb06.dbr", this.difficulty);
        this.cbRockyCoast.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineb04.dbr", this.difficulty);
        this.cbCronleysHideout.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineb02.dbr", this.difficulty);
        this.cbOldArkovia.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineb03.dbr", this.difficulty);
        this.cbArkovianUndercity.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineb05.dbr", this.difficulty);
        this.cbBrokenHills.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineb07.dbr", this.difficulty);
        this.cbStepsOfTorment.setEnabled(info.active);
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRestoredShrinesList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharShrineList.UID_SHRINE_02_SPINED_COVE)) {
                this.cbSpinedCove.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_02_ROCKY_COAST)) {
                this.cbRockyCoast.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_02_CRONLEYS_HIDEOUT)) {
                this.cbCronleysHideout.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_02_OLD_ARKOVIA)) {
                this.cbOldArkovia.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_02_ARKOVIAN_UNDERCITY)) {
                this.cbArkovianUndercity.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_02_BROKEN_HILLS)) {
                this.cbBrokenHills.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_02_STEPS_OF_TORMENT)) {
                this.cbStepsOfTorment.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharShrineAct2Pane.this.changed = true;
        }
    }
}
